﻿using CCWin;
using COMMON.Proxy;
using 易保全;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EBQComm;
using System.Drawing.Imaging;
using System.IO;

namespace 易保全
{
    public partial class IEForm : CCSkinMain
    {       
        private MainForm winParent;//父窗口
        static string ProxyAddr = AppInfo.ProxyAddr;
        IEProxy IEProxy = new IEProxy(ProxyAddr);
        private long ShowTime = AppInfo.MaxTakePreserveMins * 60;

        private bool hasLog = false;

        public IEForm(MainForm pw)
        {
            InitializeComponent();
            webBrowser1.ScriptErrorsSuppressed = true;
            this.winParent = pw;
        }

        private void FrmIE_Load(object sender, EventArgs e)
        {
            ((ExtendedWebBrowser)webBrowser1).BeforeNewWindow += new EventHandler<WebBrowserExtendedNavigatingEventArgs>(webBrowser1_BeforeNewWindow);
            webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
            IEProxy.RefreshIESettings(ProxyAddr);
            this.tbUrl.Text = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd=%E5%8C%97%E4%BA%AC%E6%97%B6%E9%97%B4&oq=%E5%8C%97%E4%BA%AC%E6%97%B6%E9%97%B4%E6%A0%A1%E5%87%86&rsv_pq=b06243380001b578&rsv_t=14eeP0pmNYCCIaDPdyCtmZ5mwN034LulVC1ury1T2Sm1GAoeljKZ04BjYvA&rqlang=cn&rsv_enter=1&rsv_sug3=14&rsv_sug1=11&rsv_sug2=0&rsv_slog=enter_key&inputT=2713&rsv_sug4=3423&rsv_sug=2&qq-pf-to=pcqq.c2c";
            this.Text = AppInfo.IETitle;

            btnRefresh_Click(null, null);
        }

        private void wb_DocumentCompleted(object sender, EventArgs e)
        {
            pictureBox2.Image= global::EBQ.Properties.Resources.done;
        }
        void webBrowser1_BeforeNewWindow(object sender, WebBrowserExtendedNavigatingEventArgs e)
        {
            e.Cancel = true;
            ((ExtendedWebBrowser)sender).Navigate(e.Url);
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (this.tbUrl.Text.Trim().Length > 0)
            {
                Cursor tmp = this.Cursor;

                this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
                this.webBrowser1.Navigate(this.tbUrl.Text.Trim());

                this.Cursor = tmp;
            }
        }

        private void tbUrl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnRefresh_Click(null, null);
            }
        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            this.tbUrl.Text = this.webBrowser1.Url.ToString();
            Cursor = Cursors.Default;
            pictureBox2.Image = global::EBQ.Properties.Resources.loading;

        }

        private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
        {
            WebBrowser browser = (WebBrowser)sender;
            HtmlElement h = browser.Document.ActiveElement;
            try
            {
                string link = h.GetAttribute("href");
                webBrowser1.Url = new Uri(link);//在本窗口中打开链接
                e.Cancel = true;//取消默认的事件
            }
            catch (Exception ex)//如果是javascript 打开的新窗口的话将会触发异常,这样的情况暂时没做处理
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            this.webBrowser1.GoForward();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //取消取证
            IEProxy.RefreshIESettings("");
            winParent.cancleTask();
            this.Close();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            //停止取证
            IEProxy.RefreshIESettings("");
            winParent.stopTask();
            this.Close();
        }

        private void FrmIE_Resize(object sender, EventArgs e)
        {
            
            int delpha_w = this.Width - 979;
            int delpha_h = this.Height - 558;

            if (delpha_w > 0)
            {
                TitleOffset = new Point((int)(delpha_w * 0.5 + 300), 5);
                this.pictureBox3.Location = new Point(delpha_w + 710, 30);
                this.btnCancel.Location = new Point(delpha_w + 740, 30);
                this.btnStop.Location = new Point(delpha_w + 770, 30);
                this.btnMark.Location = new Point(delpha_w + 800, 30);
                this.lblRemainTime.Location = new Point(delpha_w + 834, 30);
                this.tbUrl.Size = new Size(this.Width - 600, 21);
                if (delpha_h > 0)
                    this.webBrowser1.Size = new Size(this.Width, delpha_h + 480);
                this.copyrightLbl.Location = new Point((int)((this.Width - 242) / 2), this.Height -21);
            }            
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.webBrowser1.GoBack();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            if (this.tbUrl.Text.Trim().Length > 0)
            {
                Cursor tmp = this.Cursor;

                this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
                this.webBrowser1.Navigate(this.tbUrl.Text.Trim());

                this.Cursor = tmp;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!BrowserChecker.Check() && !AppInfo.MsgShown)
            {
                AppInfo.Cancled = true;
                new FrmMsg("系统提示", "你使用了非内置浏览器，本次取证无效。请重新进行取证操作").ShowDialog();
                this.btnCancel_Click(null, null);
            }
            ShowTime--;
            this.lblRemainTime.Text = string.Format("剩余取证时间:{0:00}分{1:00}秒", (int)ShowTime / 60, ShowTime % 60);

            if (ShowTime == 0)
            {
                btnCancel_Click(null, null);
            }
            else if (ShowTime == 60)
            {
                new FrmMsg("敬请注意", "您的取证时间还剩1分钟,到时录像机将自动终止!\n请您在自动终止前及时保全录像.\n自动终止后将不能进行保全!").ShowDialog();
            }
        }

        private void btnMark_Click(object sender, EventArgs e)
        {
            BorderForm body = new BorderForm();
            body.ShowDialog();
        }

        private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            Cursor = Cursors.WaitCursor;
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            /**
             * 1.截图
             * 2.提示截图成功
             * 3.写日志
             * 4.更改全局变量值
             **/
            ScreenRecorderHelper srh =   winParent.getScreenRecorderHelper();
            String path = srh.recordsPath;
            Image baseImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Graphics g = Graphics.FromImage(baseImage);
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
            g.Dispose();
            DateTime now = DateTime.Now;
            String snapshot = path + "/" + now.ToString("yyyy-MM-dd_HHmmss") + ".jpg";
            baseImage.Save(snapshot, ImageFormat.Jpeg);

            String logPath = path + "/" + "截图日志.txt";

            String logInfo = "你于 " + now.ToString("yyyy-MM-dd HH:mm:ss") + " 进行取证截图,当前浏览器地址为: " + tbUrl.Text + " 截图文件为: " + snapshot;
            
            StreamWriter sw = new StreamWriter(logPath,true);
            sw.WriteLine(logInfo);
            sw.Flush();
            sw.Close();

            new FrmMsg("系统提示", "写入截图日志成功").ShowDialog();
            hasLog = true;
        }
    }
}
